package cn.com.anypay.manager.controller.sdk;

import cn.com.anypay.manager.service.OssService;
import cn.com.anysdk.oss.exception.OssException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/oss")
public class OssController {
    @Resource
    private OssService ossService;

    @PostMapping("/switch")
    public ResponseEntity<String> switchProvider(@RequestBody Map<String, Object> config) {
         try {
            String result = ossService.switchProvider(config);
            return ResponseEntity.ok(result);
        } catch (OssException e) {
            return ResponseEntity.status(500).body("Error switching provider: " + e.getMessage());
        }
    }

    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<Map<String,Object>> uploadFile(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "isPublic", defaultValue = "true") boolean isPublic,
            @RequestParam(value = "path", required = false) String path
    ) {
        try {
            if (file == null || file.isEmpty()) {
                return ResponseEntity.badRequest().body(Map.of("error", "请选择要上传的文件"));
            }

            log.info("开始上传文件: {}, 大小: {} bytes, 是否公开: {}",
                file.getOriginalFilename(), file.getSize(), isPublic);

            Map<String,Object> result = ossService.uploadFile(file, path, isPublic);

            log.info("文件上传成功: {}, URL: {}", file.getOriginalFilename(), result.get("url"));
            return ResponseEntity.ok(result);

        } catch (OssException e) {
            log.error("文件上传失败: {}", e.getMessage(), e);
            return ResponseEntity.status(500).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            log.error("文件上传过程中发生未知错误: {}", e.getMessage(), e);
            return ResponseEntity.status(500).body(Map.of("error", "文件上传失败: " + e.getMessage()));
        }


    }

    @PostMapping("delete/{path}")
    public ResponseEntity<Map<String,Object>> deleteFile(@PathVariable String path) {
        try {
            if (path == null || path.trim().isEmpty()) {
                return ResponseEntity.badRequest().body(Map.of(
                    "success", false,
                    "message", "文件路径不能为空"
                ));
            }

            // 将下划线转换回斜杠
            String decodedPath = path.replace('_', '/');

            Map<String, Object> result = ossService.deleteFile(decodedPath);
            return ResponseEntity.ok(result);
        } catch (OssException e) {
            log.error("文件删除失败: {}", e.getMessage(), e);
            return ResponseEntity.status(500).body(Map.of(
                "success", false,
                "message", e.getMessage()
            ));
        } catch (Exception e) {
            log.error("文件删除过程中发生未知错误: {}", e.getMessage(), e);
            return ResponseEntity.status(500).body(Map.of(
                "success", false,
                "message", "文件删除失败: " + e.getMessage()
            ));
        }
    }






}
